package com.xiaomi.controller;

import com.alibaba.fastjson.JSON;
import com.xiaomi.entity.ShopSpu;
import com.xiaomi.model.Result;
import com.xiaomi.service.ShopSpuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ShopSpuController
 * @Description TODO
 * @Author yp
 * @Date 2021/11/28 10:08
 * @Version 1.0
 **/
@Api("spu")
@Controller
@RequestMapping(value = "/xiaomi", produces = "application/json; charset=utf-8")
public class ShopSpuController {
    @Autowired
    private ShopSpuService spuService;

    @ApiModelProperty("spu-search")
    @GetMapping("/spu/search.do")
    @ResponseBody
    public String pageSearch(@RequestParam Integer page,
                             @RequestParam Integer limit,
                             @RequestParam(required = false) String spuName,
                             @RequestParam(required = false) String fenLeiName,
                             @RequestParam(required = false) BigDecimal price) throws Exception {
        Map<String,Object> pars = new HashMap();
        pars.put("spuName",spuName);
        pars.put("fenLeiName",fenLeiName);
        pars.put("price",price);
        if (page !=null && limit !=null){
            pars.put("offset",(page-1)*limit);
            pars.put("limit",limit);
        }
        List<ShopSpu> List = spuService.pageSearch(pars);
        Integer count = spuService.getCount(pars);
        Result result = new Result();
        result.setCode(0);
        result.setCount(count);
        result.setData(List);
        String json = JSON.toJSONString(result);
        return json;
    }

    @ApiModelProperty("spu-增改")
    @GetMapping("/spu/insert.do")
    @ResponseBody
    public String insert(ShopSpu shopSpu) throws Exception {
      spuService.insert(shopSpu);
      Result result = new Result();
      result.setCode(0);
      result.setMsg("新增成功");
        String json = JSON.toJSONString(result);
        return json;
    }
    @ApiModelProperty("spu-增改")
    @GetMapping("/spu/update.do")
    @ResponseBody
    public String update(ShopSpu shopSpu) throws Exception {
        spuService.update(shopSpu);
        Result result = new Result();
        result.setCode(0);
        result.setMsg("修改成功");
        String json = JSON.toJSONString(result);
        return json;
    }
    @ApiModelProperty("spu-删")
    @GetMapping ("spu/del.do")
    @ResponseBody
    public String delete(@RequestParam(required = false) Integer id) throws Exception {
        //传输数据
        spuService.delete(id);
        //封装
        Result result = new Result();
        result.setCode(0);
        String json = JSON.toJSONString(result);
        return json;
    }

}
